package lcr;

import java.util.Deque;
import java.util.LinkedList;

public class Test0148 {
}

class Solution0148_1 {
    public boolean validateBookSequences(int[] putIn, int[] takeOut) {
        int length1 = putIn.length;
        int length2 = takeOut.length;

        if (length1 < length2) return false;
        else if (length2==0) return true;

        Deque<Integer> deque=new LinkedList<Integer>();

        int index=0;
        int index2 = 0;
        for (; index2 < takeOut.length; index2++) {
            if (!deque.isEmpty()&& deque.peekLast()==takeOut[index2]){
                deque.pollLast();
                continue;
            }

            while (index<length1 && putIn[index]!=takeOut[index2]){
                deque.offer(putIn[index++]);
            }

            index++;
        }

        return index==length1;
    }
}
